﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MapEditor
{
    public partial class FormNewMap : Form
    {
        const int normalHeight = 212;
        const int previewheight = 600;
        const int normalWidth = 273;
        const int mazeWidth = 546;

        public int sizeX;
        public int sizeY;

        public FormNewMap()
        {
            InitializeComponent();
            this.Width = normalWidth;
            this.Height = normalHeight;
        }

        public FormNewMap(MapData map)
        {
            InitializeComponent();
            this.Height = normalHeight;
            buttonValidate.Text = "Editer";
            textBoxName.Text = map.name;
            comboBoxType.Enabled = false;
            comboBoxType.SelectedIndex = map.type - 1;
            comboBoxDecor.SelectedIndex = (int)map.decor;
            textBoxSizeX.Text = map.sizeX.ToString();
            textBoxSizeY.Text = map.sizeY.ToString();
            if (comboBoxType.SelectedIndex == 0)
            {
                comboBoxMazeType.SelectedIndex = map.generationParameters[0];
                trackBarFreq.Value = map.generationParameters[1];
                trackBarLengh.Value = map.generationParameters[2];
                textBoxMapLengh.Text = map.generationParameters[3].ToString();
            }
            else
                this.Width = normalWidth;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if ((int.TryParse(textBoxSizeX.Text, out sizeX)) &&
                (int.TryParse(textBoxSizeY.Text, out sizeY)) &&
                comboBoxDecor.SelectedIndex != -1)
            {
                DialogResult = DialogResult.OK;
                Close();
            }
            else
                labbelError.Text = "parsing error";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Height = normalHeight;
            if (comboBoxType.SelectedIndex == 0)
            {
                this.Width = mazeWidth;
            }
            else
            {
                this.Width = normalWidth;
            }
        }

        private void buttonPreview_Click(object sender, EventArgs e)
        {
            int lengh;
            if (Int32.TryParse(textBoxMapLengh.Text, out lengh) == false)
                return;
            Graphics g = panelPreview.CreateGraphics();
            mazeSquare[,] map = MazeGenerator.generateMaze(comboBoxMazeType.SelectedIndex, trackBarFreq.Value, trackBarLengh.Value, lengh);
            g.FillRectangle(new SolidBrush(Color.White), 0, 0, 800, 800);
            if (map == null)
                return;
            this.Height = previewheight;
            int squareSize = (panelPreview.Height / map.GetLength(0)) - 1;
            int squareSize2 = (panelPreview.Width / map.GetLength(1)) - 1;
            squareSize = squareSize < squareSize2 ? squareSize : squareSize2;
            Pen blackpen = new Pen(Color.Black, (squareSize / 10 > 0) ? squareSize / 10 : 1);

            for (int i = 0; i < map.GetLength(0); i++)
                for (int j = 0; j < map.GetLength(1); j++)
                {
                    int posX = j * squareSize;
                    int posY = i * squareSize;
                    int centerX = posX + squareSize / 2;
                    int centerY = posY + squareSize / 2;
                    if ((map[ i, j] & mazeSquare.north) != 0)
                        g.DrawLine(blackpen, centerY, centerX, centerY, posX);
                    if ((map[i, j] & mazeSquare.south) != 0)
                        g.DrawLine(blackpen, centerY, centerX, centerY, centerX + squareSize / 2);
                    if ((map[i, j] & mazeSquare.east) != 0)
                        g.DrawLine(blackpen, centerY, centerX, posY, centerX);
                    if ((map[i, j] & mazeSquare.west) != 0)
                        g.DrawLine(blackpen, centerY, centerX, centerY + squareSize / 2, centerX);
                    if ((map[i, j] & mazeSquare.start) != 0)
                        g.DrawEllipse(blackpen, posY, posX, squareSize, squareSize);
                    if ((map[i, j] & mazeSquare.end) != 0)
                        g.DrawRectangle(blackpen, posY, posX, squareSize, squareSize);
                        
                }
        }

    }
}
